<?php

class Login{	
	function checkLogin(){
		if($_SESSION['user']['steamID']){
			return true;
		}
		else{
			false;
		}
	}
	
	function checkLogin2(){
		$Hash = new Hash();
		// wenn Session gesetzt dann bereits eingeloggt
		if(isset($_SESSION['user']) && $_SESSION['user']['steamID'] > 0){
			if($_COOKIE['loginHash'] != ""){
				$steamID = $Hash->checkHash($_COOKIE['loginHash']);
				if($steamID){
					return true;
				}
				else{
					return false;
				}
			}
			else{
				return false;
			}
		}
		// sonst Browser geschlossen ect -> Cookie kontrollieren
		else{
			if(isset($_COOKIE['loginHash'])){
				$steamID = $Hash->checkHash($_COOKIE['loginHash']);
					
				// wenn Hash in Datenbank -> dann einloggen
				if($steamID){
					$_SESSION['user']['steamID'] = $steamID;
					$_SESSION['debug'] = p($_SESSION['user'],1);
					return true;
				}
			}
			// Hash Manipuliert -> neu setzen
			else{
				return false;
			}
			
		}
	}
	
	function handleLogin(){
		require DR.HOME_PATH_REL.'/inc/openid.php';
		//echo "<pre>".print_r($_POST,1)."</pre>";
			try {
				# Change 'localhost' to your domain name.
				$openid = new LightOpenID(HOME_PATH);
				if(!$openid->mode) {
						if(isset($_GET['login'])) {
								$openid->identity = 'http://steamcommunity.com/openid';
								header('Location: ' . $openid->authUrl());
						}
				 } elseif($openid->mode == 'cancel') {
					echo 'User has canceled authentication!';
					} else {
						if(!$this->checkLogin2()){
							if($openid->validate()) {
											$DB = new DB();
											$User = new User();
											$con = $DB->conDB();
											$id = $openid->identity;
											// identity is something like: http://steamcommunity.com/openid/id/76561197994761333
											// we only care about the unique account ID at the end of the URL.
											$ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
											preg_match($ptn, $id, $matches);
											$steamID = $matches[1];
											//echo $steamID;
											
											//setcookie("login[steamID]", $steamID, time()+36000);
											
											// Check User already in DB
											
											// if first login generate Stats
											$firstLoginModal = "";
													if(!$User->userAlreadyInDB($steamID)){
														// neuen User eintragen und hash cookie setzen
														$User->insertNewUser($steamID);
														
														$this->insertFirstStatsOfUser($steamID);
														$firstLoginModal = "?firstLogin=1";
														header('Location: ' . HOME_PATH.$firstLoginModal);
													}
													else{
														$ret = $User->updateUser($steamID);
														$_SESSION['debug'] .= $ret['debug'];
														header('Location: ' . HOME_PATH);
													}
											
											
							} else {
											echo "User is not logged in.\n";
							}
						}
					}
			} catch(ErrorException $e) {
					echo $e->getMessage();
			}

	}
	
	function handleLogout(){
		$ret = array();
		//if($_POST["logout"]){
			//setcookie("login[steamID]", "", time()-3600);
			//unset($_COOKIE["login"]['steamID']);
			
			setcookie("loginHash", "", time()-3600);
			unset($_COOKIE['loginHash']);
			
			unset($_SESSION['debug']);
			unset($_SESSION['user']);
			
			$url = HOME_PATH;
			
			$ret['status'] = true;
			$ret['url'] = $url;
		//}
			return $ret;
	}
	

	function insertFirstStatsOfUser($steamID){
		$DB = new DB();
		$con = $DB->conDB();
		$DotaBuff = new DotaBuff();
		$UserElo = new UserElo();
		
// 		$stats = $DotaBuff->getStats($steamID);
// 		//p($stats);
		
		$insertArray["SteamID"] = secureNumber($steamID);
		$insertArray["WinLoseRatio"] = 0;
		$insertArray["Rank"] = START_ELO;
		$insertArray["Lvl"] = $stats['lvl'];
		$insertArray['Wins'] =0;
		$insertArray['Loses'] = 0;
		$DB->insert("UserStats", $insertArray);
		
		//  alle Elo's für alle MatchModes/MatchTypes eingetragen
		$status = $UserElo->insertFirstElo($steamID, false);
		//p($status);
		
		// Umleitung zur tollen Begrüßungspage
		
	}
	
}

?>